﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BigDataFramework.Helpers;
using System.Web;
using BigData.DataContracts.Objects.Reddit;
using BigData.DataContracts.Objects.BigData;
using MongoDB.Bson;
using BigDataFramework.Library.Helpers;

namespace BigDataFramework.Library.Objects.DataAdapters.Reddit
{
    public class CommentsAdapter : DataAdapters.Reddit.RedditAdapterBase<Comment>
    {
        /// <summary>
        /// No password is required for comments
        /// </summary>
        /// <param name="user"></param>
        public CommentsAdapter() { }

        /// <summary>
        /// Returns a list of comment objects
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        protected override List<Comment> RetrieveDataFromAPIForUser(BigDataUser user, DateTime startDate, DateTime endDate)
        {
            // Pull the user's reddit data from the reddit user table
            BigDataUserToRedditUserData redditUserData = this.RetrieveRedditUserIdForBigDataUserId(user);

            //Fire off the request
            RestSharp.RestRequest request = new RestSharp.RestRequest(RestSharp.Method.GET);
            request.Resource = string.Format("user/{0}/comments/.json", redditUserData.RedditUserId);
            return GetAll<Comment>(user, startDate, endDate, request);
        }

        public override Event ConvertObjToEvent(Comment obj)
        {
            // Create an event from the BigDataFourSquareCheckin details
            Event output = new Event()
            {
                Headline                        = string.Format("Commented on '{0}'", obj.link_title),
                Text                            = obj.body,
                AffiliatedUserIds               = obj.AssociatedUserIds.Select<string, ObjectId>(uid => ObjectId.Parse(uid)).ToList(),
                StartDate                       = DateTimeHelper.FromUnixTimeStamp(obj.created_utc),
                EndDate                         = DateTimeHelper.FromUnixTimeStamp(obj.created_utc),
                RecordStartDate                 = DateTime.Today,
                RecordEndDate                   = DateTime.MaxValue,
                RegisteredCollectionId          = this.RegisteredCollectionId,
                RegisteredCollectionObjectId    = ObjectId.Parse(obj._id),
            };

            return output;
        }

    }
}
